package com.dsh.m.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dsh.m.model.Goods;
import com.dsh.m.util.JsonObj;
import com.dsh.m.util.redis.Cache;
import com.dsh.m.util.redis.Redis;

@Service
public class ShoppingCartService {
	
	@Autowired
	private GoodsService goodsService;
	
	public JSONArray loadUserCart(Integer userid) {
		Cache cache = Redis.use();
		List<JsonObj> list=new ArrayList<JsonObj>();
		String hkey = "shoppingcart:"+userid;
		Set<String> keys = cache.hkeys(hkey);
		for(String key:keys) {
			Integer goodsid = Integer.parseInt(key);
			Goods goods = goodsService.getCacheGoodsById(goodsid);
			Integer num = Integer.parseInt(cache.hget(hkey, goodsid+""));
			JsonObj json = new JsonObj();
			json.setGoods(goods);
			json.setNum(num);
		    String  object = cache.get(hkey+goodsid);
			json.setBeizhu(object);
			list.add(json);
		}
		JSONArray quickSortByList = quickSortByList(list);
		//将得到数组按照根据goodsid排序
		return quickSortByList;
	}
	public JSONArray loadUserandCartByclassfy(Integer userid,Integer classfy) {
		Cache cache = Redis.use();
		String hkey = "shoppingcart:"+userid;
		Set<String> keys = cache.hkeys(hkey);
		List<JsonObj> list=new ArrayList<JsonObj>();
		for(String key:keys) {
			Integer goodsid = Integer.parseInt(key);
			Goods goods = goodsService.getCacheGoodsById(goodsid);
			Integer num = Integer.parseInt(cache.hget(hkey, goodsid+""));
			JsonObj json = new JsonObj();
			json.setGoods(goods);
			json.setNum(num);
		    String  object = cache.get(hkey+goodsid);
			json.setBeizhu(object);
			list.add(json);
		}
		JSONArray quickSortByList = quickSortByList(list);
		return quickSortByList;
	}
	
	public int getCartNum(Integer userid) {
		Cache cache = Redis.use();
		String hkey = "shoppingcart:"+userid;
		return cache.hlen(hkey).intValue();
	}
	
	public void clearUserCart(Integer userid) {
		Cache cache = Redis.use();
		String hkey = "shoppingcart:"+userid;
		cache.del(hkey);
	}
	
	public void clearRemark(Integer userid,Integer goodsid) {
		Cache cache = Redis.use();
		String hkey = "shoppingcart:"+userid+goodsid;
		cache.del(hkey);
	}

	public JSONArray quickSortByList(List<JsonObj> args) {

		for (int i = 0; i < args.size() - 1; i++) {
			for (int j = 1; j < args.size() - i; j++) {
				JsonObj a;
				if ((args.get(j - 1).getGoods().getGoodsid()).compareTo(args
						.get(j).getGoods().getGoodsid()) >0) { // 比较两个整数的大小

					a = args.get(j - 1);
					args.set((j - 1), args.get(j));
					args.set(j, a);
				}
			}
		}

		JSONArray array = new JSONArray();
		for (int k = 0; k < args.size(); k++) {

			Integer goodsid = args.get(k).getGoods().getGoodsid();
			Goods goods = goodsService.getCacheGoodsById(goodsid);
			JSONObject json = new JSONObject();
			json.put("goods", args.get(k).getGoods());
			json.put("num", args.get(k).getNum());
			json.put("beizhu", args.get(k).getBeizhu());
			array.add(json);
		}
		return array;

	}
	
}
